package com.prestamomo.vil.ppa.pmvilpage.pmfg;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilGoods;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.PmvilStillVy;
import com.prestamomo.vil.ppa.pmvilpage.pmfitter.PmvilGoodsFiter;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.PmvilFgRoot;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class PmvilGoodsFg extends PmvilFgRoot {

    private TextView goodsPmvilTvdh;
    private TextView goodsPmvilTvyh;
    private TextView goodsPmvilTvjj;
    private SmartRefreshLayout goodsPmvilSrlt;
    private RecyclerView goodsPmvilReley;

    private int pmvilOrdType =0;
    @Override
    public int setPmvilLayoutId() {
        return R.layout.fg_goods_pmvil_ly;
    }

    @Override
    public void findPmvilViewById(View view) {
        goodsPmvilTvdh = view.findViewById(R.id.goods_pmvil_tv_dh);
        goodsPmvilTvyh = view.findViewById(R.id.goods_pmvil_tv_yh);
        goodsPmvilTvjj = view.findViewById(R.id.goods_pmvil_tv_jj);
        goodsPmvilSrlt = view.findViewById(R.id.goods_pmvil_srlt);
        goodsPmvilReley = view.findViewById(R.id.goods_pmvil_reley);
    }

    @Override
    public void setViewPmvilData(View view) {
        goodsPmvilSrlt.setOnRefreshListener(refreshLayout -> {
            getGoodsPmvilList();
            goodsPmvilSrlt.finishRefresh(450);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getGoodsPmvilList();
    }

    @Override
    public void setClickPmvilEvent(View view) {
        goodsPmvilTvdh.setOnClickListener(v -> checkPmvilOrder(0));
        goodsPmvilTvyh.setOnClickListener(v -> checkPmvilOrder(1));
        goodsPmvilTvjj.setOnClickListener(v -> checkPmvilOrder(2));
    }

    private void getGoodsPmvilList(){
        pmvilAtyRoot.showPmvilLoading();
        PmvilHttpRequest.getGoodsPmvilList(this, new PmvilCallBack<PmvilGoods>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilGoods pmvilContent) {
                pmvilAtyRoot.hindPmvilLoading();
                List<PmvilGoods.LoanOrderRecordsBean> pmvilwayList = new ArrayList<>();
                if(pmvilOrdType==0){
                    for (PmvilGoods.LoanOrderRecordsBean loanOrderRecord : pmvilContent.getLoanOrderRecords()) {
                        switch (loanOrderRecord.getApplyStatus()){
                            case "0":
                            case "1":
                            case "3":
                            case "5":
                            case "6":
                            case "7":
                            case "9":
                                pmvilwayList.add(loanOrderRecord);
                                break;
                        }
                    }
                    setPmvilRelyAdpt(pmvilwayList);
                }else if(pmvilOrdType==1){
                    for (PmvilGoods.LoanOrderRecordsBean loanOrderRecord : pmvilContent.getLoanOrderRecords()) {
                        if(loanOrderRecord.getApplyStatus().equals("8")){
                            pmvilwayList.add(loanOrderRecord);
                        }
                    }
                    setPmvilRelyAdpt(pmvilwayList);
                }else  {
                    for (PmvilGoods.LoanOrderRecordsBean loanOrderRecord : pmvilContent.getLoanOrderRecords()) {
                        if(loanOrderRecord.getApplyStatus().equals("2")||loanOrderRecord.getApplyStatus().equals("4")||loanOrderRecord.getApplyStatus().equals("10")){
                            pmvilwayList.add(loanOrderRecord);
                        }
                    }
                    setPmvilRelyAdpt(pmvilwayList);

                }

            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilGoods pmvilCtent) {
                pmvilAtyRoot.outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilGoods pmvilCnt, String pmvilCode, String pmvilMsg) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                pmvilAtyRoot.tostPmvilErrMsg(pmvilError);
            }
        });
    }

    private void setPmvilRelyAdpt(List<PmvilGoods.LoanOrderRecordsBean> pmvilwayList) {
        PmvilGoodsFiter pmvilGoodsFiter = new PmvilGoodsFiter(pmvilAtyRoot, pmvilwayList);
        goodsPmvilReley.setAdapter(pmvilGoodsFiter);
        pmvilGoodsFiter.setPmvilGdsClick(pmvilGdsProduct -> {
            startActivity(new Intent(pmvilAtyRoot, PmvilStillVy.class).putExtra("gds_pmvil",pmvilGdsProduct));
        });
    }

    private void checkPmvilOrder(int pmvilGoodsType) {
        if(pmvilOrdType==pmvilGoodsType){
            return;
        }else {
            pmvilOrdType = pmvilGoodsType;
            if(pmvilGoodsType==0){
                goodsPmvilTvdh.setTextColor(Color.WHITE);
                goodsPmvilTvdh.setBackgroundResource(R.drawable.sp_pm_left_top_bott_10);
                goodsPmvilTvdh.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                goodsPmvilTvyh.setTextColor(Color.parseColor("#FE8641"));
                goodsPmvilTvyh.setTypeface(null);
                goodsPmvilTvyh.setBackgroundResource(0);
                goodsPmvilTvjj.setTextColor(Color.parseColor("#FE8641"));
                goodsPmvilTvjj.setTypeface(null);
                goodsPmvilTvjj.setBackgroundResource(0);
            }else if(pmvilGoodsType==1){
                goodsPmvilTvdh.setTextColor(Color.parseColor("#FE8641"));
                goodsPmvilTvdh.setTypeface(null);
                goodsPmvilTvdh.setBackgroundResource(0);
                goodsPmvilTvyh.setTextColor(Color.WHITE);
                goodsPmvilTvyh.setBackgroundResource(R.drawable.sp_pm_hes);
                goodsPmvilTvyh.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                goodsPmvilTvjj.setTextColor(Color.parseColor("#FE8641"));
                goodsPmvilTvjj.setTypeface(null);
                goodsPmvilTvjj.setBackgroundResource(0);
            }else{
                goodsPmvilTvdh.setTextColor(Color.parseColor("#FE8641"));
                goodsPmvilTvdh.setTypeface(null);
                goodsPmvilTvdh.setBackgroundResource(0);
                goodsPmvilTvyh.setTextColor(Color.parseColor("#FE8641"));
                goodsPmvilTvyh.setTypeface(null);
                goodsPmvilTvyh.setBackgroundResource(0);
                goodsPmvilTvjj.setTextColor(Color.WHITE);
                goodsPmvilTvjj.setBackgroundResource(R.drawable.sp_pm_right_top_bott_10);
                goodsPmvilTvjj.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
            getGoodsPmvilList();
        }
    }
}
